/*
 * @(#)MaintWorkflowProgressAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.workflow.bean.MtmWorkflowProgressPriorityRule;
import com.dcivision.workflow.bean.MtmWorkflowStepPriorityRule;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.bean.WorkflowStepRoute;
import com.dcivision.workflow.core.WorkflowPermissionManager;
import com.dcivision.workflow.dao.MtmWorkflowStepPriorityRuleDAObject;
import com.dcivision.workflow.dao.MtmWorkflowStepUserActorDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;
import com.dcivision.workflow.dao.WorkflowStepRouteDAObject;

/**
  MaintWorkflowProgressAction.java

  This class is for maint wprkflow record.

    @author          Angus Shiu
    @company         DCIVision Limited
    @creation date   15/01/2004
    @version         $Revision: 1.12 $
*/
public class MaintWorkflowProgressAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.12 $";

  /** Creates a new instance of MaintWorkflowProgressAction */
  public MaintWorkflowProgressAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_TASK);
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowProgressDAObject");
  }

  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintWorkflowProgressForm workflowProgressForm = (MaintWorkflowProgressForm) form;
    workflowProgressForm.setID(null);
  }

  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintWorkflowProgressForm workflowProgressForm = (MaintWorkflowProgressForm) form;
    Integer workflowStepID = TextUtility.parseIntegerObj(workflowProgressForm.getWorkflowStepID());
    Integer trackID = TextUtility.parseIntegerObj(workflowProgressForm.getTrackID());
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection dbConn = this.getConnection(request);

    WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(sessionCon, dbConn);
    WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionCon, dbConn);
    MtmWorkflowStepUserActorDAObject stepActorDAO = new MtmWorkflowStepUserActorDAObject(sessionCon, dbConn);
    MtmWorkflowStepPriorityRuleDAObject priorityRuleDAO = new MtmWorkflowStepPriorityRuleDAObject(sessionCon, dbConn);
    WorkflowPermissionManager wfPermManager = new WorkflowPermissionManager(sessionCon, dbConn); 
    String permissionTypes = null;
    WorkflowStep step = (WorkflowStep) stepDAO.getObjectByID(workflowStepID);
    if (GlobalConstant.TRUE.equals(step.getAllowAssignPriority())){
      List priorityRuleList = priorityRuleDAO.getListByWorkflowStepID(step.getID());
      String priorityRuleListValue = null;
      String priorityRule = "";
log.debug ( "priorityRuleList.size()="+priorityRuleList.size());
      for (int i = 0; i < priorityRuleList.size(); i++){
        MtmWorkflowStepPriorityRule stepPriorityRule = (MtmWorkflowStepPriorityRule)priorityRuleList.get(i);
        priorityRule = stepPriorityRule.getEscalateAtAmt() + "," +
          stepPriorityRule.getEscalateAtField() + "," +
          stepPriorityRule.getEscalateTrigger() + "," +
          stepPriorityRule.getEscalateLevel() + ",";
        if (Utility.isEmpty(stepPriorityRule.getEscalateAckMethod())){
          priorityRule += "null,null";
        } else if (stepPriorityRule.getEscalateAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL) > -1 &&
                stepPriorityRule.getEscalateAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM) > -1) {
          priorityRule += stepPriorityRule.getEscalateAckMethod();
        } else if (stepPriorityRule.getEscalateAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL) > -1){
          priorityRule += MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL + ",null";
        } else if (stepPriorityRule.getEscalateAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM) > -1){
          priorityRule += "null," + MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM;
        }
log.debug("priorityRule="+priorityRule);

        if ( Utility.isEmpty(priorityRuleListValue) ) {
          priorityRuleListValue = priorityRule;
        } else {
          priorityRuleListValue += "|" + priorityRule;
        }
      }
log.debug("priorityRuleListValue="+priorityRuleListValue);
      step.setPriorityRuleListValue(priorityRuleListValue);
    }
    if (!Utility.isEmpty(trackID)) {
      permissionTypes = wfPermManager.getPermissionForUser(step.getID(),trackID, sessionCon.getUserRecordID());
    } else {
      permissionTypes = wfPermManager.getPermissionForUser(step.getID(), null, sessionCon.getUserRecordID());
    }
    request.setAttribute("permissionTypes", permissionTypes);
    request.setAttribute("WorkflowStep", step);

    List userActorList = stepActorDAO.getListByWorkflowStepID(step.getID());
    request.setAttribute("UserActorList", userActorList);
    List progressList = new ArrayList();

   // get possible decision
   String[][] workflowRouteArr = null;
   if (WorkflowStep.ACTION_TYPE_USER_DEFINED_TASK.equals(step.getActionType())) {
     WorkflowStepRouteDAObject stepRouteDAO = new WorkflowStepRouteDAObject(sessionCon, dbConn);
     log.debug("workflow step id, getting workflow routes: "+workflowStepID);
     List tmpList = stepRouteDAO.getListByWorkflowStepID(workflowStepID);
     workflowRouteArr = new String[tmpList.size()+1][];
     workflowRouteArr[0] = new String[] {MessageResourcesFactory.getMessage(sessionCon.getSessionLocale(), "common.options.select_menu.select_one"), ""};
     for (int i=0; i<tmpList.size(); i++) {
       WorkflowStepRoute stepRoute = (WorkflowStepRoute) tmpList.get(i);
       workflowRouteArr[1+i] = new String[] {stepRoute.getRouteName(), WorkflowStep.PARENT_TYPE_SINGLE + stepRoute.getRouteSeq()};
     }
   }
   request.setAttribute("workflowRouteList", workflowRouteArr);

    if (!Utility.isEmpty(trackID)) {
      progressList = progressDAO.getListByStepIDAndTrackID(workflowStepID, trackID);
    }
    request.setAttribute("ExecutedProgressList", progressList);
    dbConn = null;
  }
}
